//
//  BPModifyPwdViewController.swift
//  BP_NewClient
//
//  Created by letv on 15/6/19.
//  Copyright (c) 2015年 normal. All rights reserved.
//  修改密码

import UIKit

class BPModifyPwdViewController: BPRootViewController,keyBoardTopBarDelegate{

    @IBOutlet var scrollView : UIScrollView?
    @IBOutlet var currentPwdField : UITextField?
    @IBOutlet var newPwdField : UITextField?
    @IBOutlet var confirmPwdField : UITextField?
    
    var keyBoard : KeyBoardTopBar!
    var textfieldArray : NSArray?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "修改密码"
        self.navigationItem.leftBarButtonItem = self.getBackNavigationItem()

        textfieldArray = NSArray(objects: currentPwdField!,newPwdField!,confirmPwdField!)
        keyBoard  = KeyBoardTopBar(array: textfieldArray as! [AnyObject])
        keyBoard.delegate = self
    }
    
    
    //MARK UITextFieldDelegate
    func textFieldDidBeginEditing(textField: UITextField){
        keyBoard.setAllowShowPreAndNext(true)
        keyBoard.ShowBar(textField)
        
        if textField.isEqual(newPwdField){
            scrollView?.setContentOffset(CGPointMake(0, 80), animated: true)
        }else if textField.isEqual(confirmPwdField){
            scrollView?.setContentOffset(CGPointMake(0, 140), animated: true)
        }else{
            scrollView?.setContentOffset(CGPointMake(0, 0), animated: true)
        }
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool{
        textField.resignFirstResponder()
        return true
    }
    
    // MARK keyBoardTopBarDelegate
    // 确认
    func keyBoardTopBarConfirmClicked(textField: UITextField!) {
        scrollView?.setContentOffset(CGPointMake(0, 0), animated: true)
    }
    //下一项回调
    func keyBoardTopBarNext(textField: UITextField!) {

    }
    
    //上一项回调
    func keyBoardTopBarPrevious(textField: UITextField!) {
        
    }

    @IBAction func buttonClicked(){
        if (currentPwdField?.text!.isEmpty == true){
            self.showNoticeMsg("请输入当前密码")
            return
        }else if (newPwdField?.text!.isEmpty == true){
            self.showNoticeMsg("请输入新密码")
            return

        }else if (confirmPwdField?.text!.isEmpty == true){
            self.showNoticeMsg("请再次输入新密码")
            return
        }
        
        let dic : Dictionary = ["oldpwd":currentPwdField!.text!,"newpwd":newPwdField!.text!,"confirmpwd":confirmPwdField!.text!]
        
        BPBaseRequest.shareInstance().requestPostWithParamerSuccessFailBlock(USER_MODIFY_PWD, parameter: dic, callBoolBlock: {(success: Bool) in
            self.showNoticeMsg("修改成功")
            self.navigationController?.popViewControllerAnimated(true)

            }, callFailureBlock: {(responErrorDictionary : NSDictionary!) in
                let msg:String? = responErrorDictionary.objectForKey("error_msg") as? String
                self.showNoticeMsg(msg!)
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
